package daily;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;

import org.apache.commons.lang.math.RandomUtils;

public class SemaphoreTest {

	public static void main(String[] args) {
		ExecutorService threadPool=Executors.newCachedThreadPool();
		final Semaphore semaphore=new Semaphore(3);
		for(int i=0;i<10;i++){
			Runnable runnable=new Runnable(){
				@Override
				public void run() {
					try {
						semaphore.acquire();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					
					System.out.println(System.currentTimeMillis()+"线程 "+Thread.currentThread().getName()+" 进入，当前已有 "+(3-semaphore.availablePermits())+"个并发");
					
					try {
						Thread.sleep(RandomUtils.nextInt(10000));
					} catch (InterruptedException e) {
						e.printStackTrace();
					}finally{
						System.out.println(System.currentTimeMillis()+"线程 "+Thread.currentThread().getName()+" 离开，当前已 "+(3-semaphore.availablePermits())+"个并发");
						semaphore.release();
					}
					
				}};
				
				threadPool.execute(runnable);
		}
		threadPool.shutdown();
	}

}
